﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Web;
using System.Web.Mvc;
using CommonSearchModule;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using CommonSearchModule.com.webservice.www;
using webTest.com.webservice.www;

namespace Lucene测试.Controllers
{
    public class ControllerBase : Controller
    {
        public ControllerBase()
        {
            this.PageSize = 10;
            this.SearchModule = SearchModule.Item;
        }
        /// <summary>
        /// 调用WIN API方法
        /// 非托管资源
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="def"></param>
        /// <param name="retVal"></param>
        /// <param name="size"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        /// <summary>
        /// 得到索引文件路径
        /// </summary>
        /// <param name="configPath"></param>
        /// <returns></returns>
        protected string GetIndexPath(string configPath)
        {
            StringBuilder sbPath = new StringBuilder(500);
            GetPrivateProfileString("Index", "Path", "", sbPath, 500, configPath);

            return sbPath.ToString();
        }

        /// <summary>
        /// 组成年级与科目关键字
        /// </summary>
        /// <param name="gid"></param>
        /// <param name="sid"></param>
        /// <returns></returns>
        protected virtual string GroupPropertyName(string gid, string sid)
        {
            if (!string.IsNullOrEmpty(gid) && !string.IsNullOrEmpty(sid))
                return gid + "|" + sid;
            else if (!string.IsNullOrEmpty(gid) && string.IsNullOrEmpty(sid))
                return gid;
            else if (string.IsNullOrEmpty(gid) && !string.IsNullOrEmpty(sid))
                return sid;
            else
                return string.Empty;
        }

        /// <summary>
        /// 页面显示记录数
        /// </summary>
        protected virtual int PageSize { get; set; }

        /// <summary>
        /// 从索引文件中重到数据，根据关键字
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        protected virtual SearchResult GetDataFromIndex(QueryTerm queryTerm)
        {
            QueryTerm term = new QueryTerm
            {
                KeyWord = queryTerm.KeyWord,
                PageIndex = queryTerm.PageSize,
                CategoryName = queryTerm.CategoryName,
                PropertyName = queryTerm.PropertyName,
                PageSize = this.PageSize,
            };
            switch (this.SearchModule)
            {
                case SearchModule.Ask:
                    this.GetSearchDetailPage = "/OnlineResAskInfo/DetailedList?listid={0}";
                    break;
                case SearchModule.Fudao:
                    this.GetSearchDetailPage = "/OneToOne/DetailStuRequirement/{0}";
                    break;
                case SearchModule.Item:
                    this.GetSearchDetailPage = "/Student/Detail?itemid={0}";
                    break;
                case SearchModule.JiaoAn:
                    this.GetSearchDetailPage = "/TeachingPlan/TeachingPlanInfo?pid={0}";
                    break;
                case SearchModule.ShiTi:
                    this.GetSearchDetailPage = "/QuestionsDownload/DetailsList?id={0}";
                    break;
                default:
                    break;
            }

            //通过lucene服务器得到符号条件的结果
            SearchResult searcher = (SearchResult)ByteArrayToObject(new ReadFileIndex().ReadLocalFile((int)this.SearchModule, queryTerm.KeyWord, queryTerm.CategoryName, queryTerm.PropertyName, queryTerm.PageIndex, queryTerm.PageSize));

            return searcher;
        }
        
        /// <summary>
        /// 将byte[]转换为object
        /// </summary>
        /// <param name="ba"></param>
        /// <returns></returns>
        object ByteArrayToObject(byte[] ba)
        {
            MemoryStream ms = new MemoryStream(ba);
            BinaryFormatter bf = new BinaryFormatter();
            return bf.Deserialize(ms);
        }

        /// <summary>
        /// 搜索模块
        /// </summary>
        protected SearchModule SearchModule { get; set; }
        /// <summary>
        /// 得到搜索后最终页的地址 
        /// </summary>
        protected virtual string GetSearchDetailPage { get; private set; }
    }
}